package com.auth2resource.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2ClientProperties;
import org.springframework.http.*;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Base64;
import java.util.Collections;


@RestController
public class RestControllers {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OAuth2ClientProperties oAuth2ClientProperties;
    @Autowired
    private OAuth2ProtectedResourceDetails oAuth2ProtectedResourceDetails;

    @PostMapping(value = "/login")
    public ResponseEntity<OAuth2AccessToken> login() throws Exception {
            //Http Basic 验证
            String clientAndSecret = oAuth2ClientProperties.getClientId() + ":" + oAuth2ClientProperties.getClientSecret();
            //这里需要注意为 Basic 而非 Bearer
            clientAndSecret = "Basic " + Base64.getEncoder().encodeToString(clientAndSecret.getBytes());
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.set("Authorization", clientAndSecret);

            //授权请求信息
            MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
            map.put("username", Collections.singletonList("admin"));
            map.put("password", Collections.singletonList("123456"));
            map.put("grant_type", Collections.singletonList("password"));
            map.put("scope", Collections.singletonList("app"));
            //HttpEntity
            HttpEntity httpEntity = new HttpEntity(map, httpHeaders);
            //获取 Token
            ResponseEntity<OAuth2AccessToken> body = restTemplate.exchange(oAuth2ProtectedResourceDetails.getAccessTokenUri(), HttpMethod.POST, httpEntity, OAuth2AccessToken.class);
            OAuth2AccessToken oAuth2AccessToken = body.getBody();
//            System.out.println(oAuth2AccessToken.getValue());
//            System.out.println(oAuth2AccessToken.getRefreshToken().getValue());
            return body;
    }
    @GetMapping("/refrech_token")
    public ResponseEntity<OAuth2AccessToken> refrech_token(){
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("client_id", oAuth2ClientProperties.getClientId());
        map.add("client_secret", oAuth2ClientProperties.getClientSecret());
        map.add("grant_type", "refresh_token");
        map.add("refresh_token", "e5cd3cc6-f98c-4a4e-8607-854bd2d38aed");


        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity httpEntity = new HttpEntity(map, headers);
//        Map map = restTemplate.exchange(oAuth2ProtectedResourceDetails.getAccessTokenUri(), HttpMethod.POST,
//                new HttpEntity<MultiValueMap<String, String>>(formData, headers), Map.class).getBody();
        ResponseEntity<OAuth2AccessToken> body = restTemplate.exchange(oAuth2ProtectedResourceDetails.getAccessTokenUri(), HttpMethod.POST, httpEntity, OAuth2AccessToken.class);
        OAuth2AccessToken oAuth2AccessToken = body.getBody();
        System.out.println(oAuth2AccessToken.getValue());
        System.out.println(oAuth2AccessToken.getRefreshToken().getValue());
        return body;
    }

}
